package arcane.object.screen;

import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import arcane.constant.Constants;
import arcane.jibx.io.JibxIO;
import arcane.object.game.KeyHandler;
import arcane.object.game.map.ArcanePlayField;
import arcane.object.game.map.LinkedMap;
import arcane.object.game.map.Tile;
import arcane.object.game.sprite.SorcerySpriteGroup;

import com.golden.gamedev.GameEngine;
import com.golden.gamedev.GameObject;
import com.golden.gamedev.object.PlayField;
import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.util.ImageUtil;

public class PlayScreen extends GameObject {
	LinkedMap tileBG;
	// Sprite groups
	SorcerySpriteGroup friendGroup;
	SorcerySpriteGroup friendProjectileGroup;
	SorcerySpriteGroup enemyGroup;
	SorcerySpriteGroup enemyProjectileGroup;
	
	Sprite TMP = new Sprite(ImageUtil.createImage(32, 32));

	PlayField field;
	public static final int PLAYING = 0, TRANSITION = 1, MENU = 2;
	int gameState;

	public PlayScreen(GameEngine parent) {
		super(parent);
	}
	

	@Override
	public void update(long time) {
		KeyHandler.InGameKey(bsInput.getKeyPressed());
		if(bsInput.isMouseDown(MouseEvent.BUTTON1))
			TMP.setLocation(getMouseX(), getMouseY());
		tileBG.setToCenter(TMP);
		field.update(time);
	}

	@Override
	public void initResources() {
		friendGroup = new SorcerySpriteGroup("FRIENDLY");
		friendProjectileGroup = new SorcerySpriteGroup("FRIENDLY_PROJECTILE");
		enemyGroup = new SorcerySpriteGroup("ENEMY");
		enemyProjectileGroup = new SorcerySpriteGroup("ENEMY_PROJECTILE");

		field = new ArcanePlayField();
		field.addGroup(friendGroup);
		field.addGroup(enemyGroup);
		field.addGroup(enemyProjectileGroup);
		field.addGroup(friendProjectileGroup);

		//tileBG = new LinkedMap();// TEST CODE
		try {
			tileBG = (LinkedMap) new JibxIO(LinkedMap.class).load("map1.xml",
					LinkedMap.class);
			System.out.println("map image name: "+tileBG.getImageName());
			System.out.println(System.getProperty("user.dir"));
			BufferedImage img = ImageUtil.getImage(ClassLoader
					.getSystemResource(tileBG.getImageName()),
					Transparency.TRANSLUCENT);
			tileBG.setImages(ImageUtil.splitImages(img, img.getWidth() / 32,
					img.getHeight() / 32));
			Tile[][] layers = tileBG.getLayers();
			tileBG.setSize(layers[0].length,layers.length);
		} catch (Exception e) {
			e.printStackTrace();
		}
		field.setBackground(tileBG);
		field.add(TMP);
	}

	@Override
	public void render(Graphics2D g) {
		g.setColor(Constants.CLEAR_COLOR);
		g.fillRect(Constants.ORIGIN_X, Constants.ORIGIN_Y,
				Constants.GAME_WIDTH, Constants.GAME_HEIGHT);

		// render tiles beneath sprites
		tileBG.RENDER_TILE = LinkedMap.LOWER_TILES;
		tileBG.render(g);

		// render sprites
		field.render(g);

		// Render upper tiles over sprites
		tileBG.RENDER_TILE = LinkedMap.UPPER_TILES;
		tileBG.render(g);

		switch (gameState) {
		case PLAYING:
			if (keyDown(Constants.MOVE_DOWN) && keyDown(Constants.MOVE_UP)) {
			} else if (keyDown(Constants.MOVE_LEFT)
					&& keyDown(Constants.MOVE_RIGHT)) {
			} else if (keyDown(Constants.MOVE_UP)) {
			} else if (keyDown(Constants.MOVE_DOWN)) {
			}
			break;
		case MENU:
			break;
		}
	}
}